import sys
import argparse

def convert_input_format(lines):
    """处理输入文件转换逻辑"""
    n = int(lines[0])
    c = int(lines[-1])
    items = []
    for line in lines[1:-1]:
        parts = line.split()
        items.append((parts[0], parts[1], parts[2]))  # (id, profit, weight)
    return n, c, items

def convert_output_format(original_output, input_items):
    """处理输出文件转换逻辑"""
    total_profit = int(original_output[0].strip())
    
    # 匹配选中的物品ID
    selected_ids = []
    for out_line in original_output[1:]:
        p, w = out_line.strip().split()
        for item in input_items:
            if item[1] == p and item[2] == w:
                selected_ids.append(item[0])
                break
    
    return total_profit, selected_ids

def main():
    # 文件模式处理input和output
    parser = argparse.ArgumentParser()
    parser.add_argument('--input', required=True, help='原始输入文件路径')
    parser.add_argument('--original-output', required=True, help='原始输出文件路径')
    parser.add_argument('--output-dir', required=True, help='输出目录路径')
    args = parser.parse_args()

    # 处理输入文件
    with open(args.input) as f:
        input_lines = [line.strip() for line in f if line.strip()]
    n, c, input_items = convert_input_format(input_lines)
    
    # 生成input.txt
    with open(f"{args.output_dir}/input.txt", "w") as f:
        f.write(f"{n} {c}\n")
        for item in input_items:
            f.write(f"{item[1]} {item[2]}\n")

    # 处理输出文件
    with open(args.original_output) as f:
        output_lines = [line.strip() for line in f if line.strip()]
    
    total_profit, selected_ids = convert_output_format(output_lines, input_items)
    
    # 生成output.txt
    with open(f"{args.output_dir}/output.txt", "w") as f:
        f.write(f"{total_profit}\n")
        f.write(" ".join(selected_ids))
        

if __name__ == "__main__":
    main()